﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Interface;
using Microsoft.Xna.Framework.Input.Touch;

namespace Spinning.Spinning.Entity
{
    class StageIcon : BaseEntity, ITouchable
    {
        Sprite lockSprite;
        public delegate void TouchEventHandler(BaseEntity entity, TouchLocation touchLocation);
        public TouchEventHandler OnTouchEvent, OnReleaseEvent, OnHoldEvent;
        bool _pressed;
        AnimationSprite stageIconSprite, counterSprite;
        bool _isLock;

        public int StageNumber
        {
            get;
            set;
        }

        public bool isLock
        {
            get 
            {
                return _isLock;
            }
            set 
            {
                _isLock = value;
                if (_isLock) 
                {
                    lockSprite.Alpha = 1;
                    stageIconSprite.SetChangeFrameAnimation(new int[] { 0 }); 
                }
                else 
                {
                    lockSprite.Alpha = 0;
                    stageIconSprite.SetChangeFrameAnimation(new int[] { 1 }); 
                }
            }
        }

        public override void OnLoad()
        {
            ZIndex = Constants.ui;
            stageIconSprite = new AnimationSprite();
            stageIconSprite.textureName = "singlePlayerMenu/post";  //!
            stageIconSprite.OnLoad();
            stageIconSprite.SetAnimation(new Point(2, 1));
            stageIconSprite.Position = new Vector2();
            this.Attach(stageIconSprite);

            counterSprite = new AnimationSprite();
            counterSprite.textureName = "singlePlayerMenu/number"; //!
            counterSprite.OnLoad();
            counterSprite.SetAnimation(new Point(3, 4));
            counterSprite.Position = new Vector2(10, 15);
            this.Attach(counterSprite);
            counterSprite.SetChangeFrameAnimation(new int[] { StageNumber });

            lockSprite = new Sprite();
            lockSprite.textureName = "singlePlayerMenu/lock";
            lockSprite.OnLoad();
            lockSprite.Position = new Vector2(46, 50);
            this.Attach(lockSprite);
        }

        public bool Contain(Microsoft.Xna.Framework.Vector2 position)
        {
            if (_isLock) return false;
            else
            {
                position = Vector2.Transform(position, UnProjection);
                Vector2 topLeft = Position - Origin * new Vector2(stageIconSprite._frameSize.X, stageIconSprite._frameSize.Y) * Scale;
                Vector2 rightDown = topLeft + new Vector2(stageIconSprite._frameSize.X, stageIconSprite._frameSize.Y) * Scale;
                return (position.X > topLeft.X && position.X < rightDown.X && position.Y > topLeft.Y && position.Y < rightDown.Y);
            }
        }

        public void OnTouch(TouchLocation touchLocation)
        {
            _pressed = true;
            if (OnTouchEvent != null)
                OnTouchEvent(this, touchLocation);
        }

        public void OnRelease(TouchLocation touchLocation)
        {
            _pressed = false;
            if (OnReleaseEvent != null)
                OnReleaseEvent(this, touchLocation);
        }

        public void OnHold(TouchLocation touchLocation)
        {
            if (OnHoldEvent != null)
                OnHoldEvent(this, touchLocation);
        }
    }
}
